﻿<?php

// ==================================================================================================
// 												ONTOLOGIA
// ==================================================================================================

// ==================================================================================================			
//	FUNCION - CONSULTA INFORMACION BASICA DE LA TAXONOMIA EN LA ONTOLOGIA
// ==================================================================================================

function consultar_info_ontologia_xajax( $taxonomia ) {
    $objResponse = new xajaxResponse();
	
	$store 	= $_SESSION['store'];
	
	$sql = "PREFIX rdf: <http://www.w3.org/2000/01/rdf-schema#> 
			
			SELECT ?label ?comment  
			WHERE { 
				?Class rdf:label ?label FILTER (?label = '".$taxonomia."' ).
				?Class rdf:comment ?comment.
			} 
			ORDER BY ?label";
	
	$rows = $store->query($sql, 'rows');
		
	if ($rows = $store->query($sql, 'rows')) {
		foreach ($rows as $row) {
			$label 		= $row['label'];
			$comment 	= $row['comment'];
		}
		
		$objResponse->assign("div_label", 					"innerHTML", 		$label);
		
		$switch_label = "";
		if ($_SESSION['ubicacion'] == "produccion") {
			$switch_label = utf8_decode($label);
		} else {
			$switch_label = $label;
		}
		
		$img = "";
		$sql = "";
		$tables = "";
		switch( $switch_label ) {
			case "Insecto Acuático.":	$img = "tinsecto";
										break;
						
			case "Orden.":				$instancia = "Ordenes:";
										
										$sql = "PREFIX onto:  <http://www.owl-ontologies.com/OntoInsectos.owl#>
										
												SELECT DISTINCT ?id_onto
												WHERE {
												   	?id_onto ?p1 ?o1 FILTER (?o1 = onto:Orden)
												   	?id_onto ?p2 ?o2 FILTER (?p2 = onto:Ciclo_de_Vida)
											  	}
												ORDER BY ?id_onto";
										$img = "torden";
										break;
						
			case "Familia.":			$instancia = "Familias:";
										
										$sql = "PREFIX onto:  <http://www.owl-ontologies.com/OntoInsectos.owl#>
												
												SELECT DISTINCT ?id_onto 
												WHERE {
													?familia ?fpo ?id_onto FILTER (?fpo = onto:or_compuesto_por_fa).
												}
												ORDER BY ?id_onto";
										$img = "tfamilia";
										break;
						
			case "Género.":				$instancia = "G&eacute;neros:";
										
										$sql = "PREFIX onto:  <http://www.owl-ontologies.com/OntoInsectos.owl#>
												
												SELECT DISTINCT ?id_onto 
												WHERE {
													?familia ?fpo ?orden FILTER (?fpo = onto:fa_pertenece_a_or).
													?familia ?fcg ?id_onto FILTER (?fcg = onto:fa_compuesta_por_ge).
														FILTER (?orden = onto:Plecóptera). 
														
												}
												ORDER BY ?id_onto";
										$img = "tgenero";
										break;
						
			case "Especie.":			$instancia = "Especies:";
										
										$sql = "PREFIX onto: <http://www.owl-ontologies.com/OntoInsectos.owl#> 
												
												SELECT DISTINCT ?orden ?familia ?genero ?id_onto 
												WHERE { 
													?famila ?fpo ?orden FILTER (?fpo = onto:fa_pertenece_a_or). 
													?genero ?gpf ?familia FILTER (?gpf = onto:ge_pertenece_a_fa). 
													?id_onto ?epg ?genero FILTER (?epg = onto:es_pertenece_a_ge). 
												}
												ORDER BY ?id_onto";
										$img = "tespecie";
										break;
										
										
			case "Adulto.":				$img = "tadulto";
										break;
						
			case "Abdomen Adulto.":		$img = "tabdomenadulto";
										break;
						
			case "Cabeza Adulto.":		$img = "tcabezaadulto";
										break;
						
			case "Tórax Adulto.":		$img = "ttoraxadulto";
										break;
										
										
			case "Larva.":				$img = "tlarva";
										break;
						
			case "Abdomen Larva.":		$img = "tabdomenlarva";
										break;
						
			case "Cabeza Larva.":		$img = "tcabezalarva";
										break;
						
			case "Tórax Larva.":		$img = "ttoraxlarva";
										break;
		}
		
		if ($sql != "" ) {
			if ($_SESSION['ubicacion'] == "produccion") {
				$rows = $store->query(utf8_encode($sql), 'rows');
				
				if ($rows = $store->query(utf8_encode($sql), 'rows')) {
					$i = 0;
					foreach ($rows as $row) {
						$data				= explode("#", $row['id_onto']);
						$lista_ids_onto[$i] 	= $data[1];
						$i++;
					}
				}
			} else {
				$rows = $store->query($sql, 'rows');
				
				if ($rows = $store->query($sql, 'rows')) {
					$i = 0;
					foreach ($rows as $row) {
						$data				= explode("#", $row['id_onto']);
						$lista_ids_onto[$i] 	= $data[1];
						$i++;
					}
				}
			}
			
			$filas = 0;
			for ($i = 0; $i < count($lista_ids_onto); $i++) {
				if ($i == 0) {
					$tables = 	"<table width='100%'>
									<tr>
										<td width='33%'>&nbsp;</td>
										<td width='34%'>&nbsp;</td>
										<td width='33%'>&nbsp;</td>
									</tr>
									<tr>
										<td class='text' colspan='3'>
											<b>
												".$instancia."
											</b>
										</td>
									</tr>
									<tr>";
				}
				
				if ($instancia == "Especies:") {
					$ids_onto 		= split("_", $lista_ids_onto[$i]);
					$label_consulta = str_replace("_", " ", $lista_ids_onto[$i]);
					
					$tables .= 			"<td>
											<a id='comment_onto_btn' onclick='fn_comment_onto( \"".$label_consulta."\" );'>
												&bull; ".ucfirst($ids_onto[1])."
											</a>
										</td>";
				} else {
					$tables .= 			"<td>
											<a id='comment_onto_btn' onclick='fn_comment_onto( \"".$lista_ids_onto[$i]."\" );'>
												&bull; ".$lista_ids_onto[$i]."
											</a>
										</td>";
				}
				
				if ($filas > 1) {
					$filas = 0;
					$tables .= 		"</tr>
									<tr>";
				} else {
					$filas++;
				}
				
				/*
				if ($i == (count($lista_generos)-1) ) {
					$tables .= 		"</tr>
								</table>";
				}
				*/
			}
		}
		
		if ( $tables != "" ) {
			$tables .= 		"</tr>
						</table>";
		}
		
		$objResponse->assign("div_comment", 				"innerHTML", 		$comment.$tables);
		
		if ( $img != "" ) {
			$objResponse->assign("div_grafico", 				"innerHTML", 		"<img src='../../publicos/images/taxonomia/".$img.".jpg' width='' height='' border='0' />");
		} else {
			$objResponse->assign("div_grafico", 				"innerHTML", 		"Imagen no disponible.");
		}
		
	} else {
		$objResponse->assign("div_label", 					"innerHTML", 		"ERROR EN LA CONSULTA");
		$objResponse->assign("div_comment", 				"innerHTML", 		"SQL: ".$sql);
		$objResponse->assign("div_grafico", 				"innerHTML", 		"ERROR EN LA CONSULTA");
	}
	
    return $objResponse;
}


// ==================================================================================================			
//	FUNCION - CONSULTAR DESCRIPCION EN ONTOLOGIA
// ==================================================================================================

function consultar_comment_onto_xajax( $taxonomia ) {
    $objResponse = new xajaxResponse();
	
	$store 	= $_SESSION['store'];
	
	$sql = "PREFIX rdf: <http://www.w3.org/2000/01/rdf-schema#> 
			
			SELECT ?label ?comment  
			WHERE { 
				?Class rdf:label ?label FILTER (?label = '".$taxonomia."' ).
				?Class rdf:comment ?comment.
			} 
			ORDER BY ?label";
	
	$rows = $store->query($sql, 'rows');
		
	if ($rows = $store->query($sql, 'rows')) {
		foreach ($rows as $row) {
			$label 		= $row['label'];
			$comment 	= $row['comment'];
		}
		
		$objResponse->assign("div_comment_onto_titulo", 		"innerHTML", 		$label);
		$objResponse->assign("div_comment_onto_descripcion", 	"innerHTML", 		str_replace("#", "<br/>", str_replace("/*", "<b>", str_replace("*/", "</b>", $comment))));
	
	} else {
		$objResponse->assign("div_comment_onto_titulo", 		"innerHTML", 		"ERROR EN LA CONSULTA");
		$objResponse->assign("div_comment_onto_descripcion", 	"innerHTML", 		"SQL: ".$sql);
	}
	
    return $objResponse;
}
?>